/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.GUI.actions;

import controller.commands.AddBoxNodeCommand;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import model.graphic.GraphicModel;
import model.graphic.objects.GBoxNode;
import model.graphic.objects.GNode;
import model.graphic.objects.Graphical;
import view.GUI.LabelReader;
import view.GUI.sidepanels.Palette;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Jelle
 */
public class AddBoxAction extends AbstractAction {

    private GraphicView gview;
    private GraphicModel gmodel;
    private Palette palette;
    private boolean possible = false;
    ;

    public AddBoxAction(GraphicView gview, GraphicModel gmodel, Palette palette) {
        this.gview = gview;
        this.palette = palette;
        this.gmodel = gmodel;
        
    }

    public void actionPerformed(ActionEvent e) {
        this.possible=false;
        for (Graphical g : gmodel.getSelection()) {
            if (GNode.class.isAssignableFrom(g.getClass())) {
                if (!GBoxNode.class.isAssignableFrom(g.getClass()) && !((GNode) g).isBoxed()) {
                    possible = true;
                }
            }
        }
        if (possible) {
            String name = JOptionPane.showInputDialog(gview.getParent(), 
                    LabelReader.getInstance().getString("GiveTheBoxnodeAName"),
                    LabelReader.getInstance().getString("Input"), JOptionPane.INFORMATION_MESSAGE);
            String info = (String)JOptionPane.showInputDialog(gview.getParent(),
                    LabelReader.getInstance().getString("GiveTheBoxnodeInfo"),
                    LabelReader.getInstance().getString("Input"),JOptionPane.INFORMATION_MESSAGE,
                    null,null, LabelReader.getInstance().getString("ThisIsACustomBoxnode"));
            if (name!=null && !name.equals("")) {
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                LabelReader.getInstance().addString("info" + name, info);
                gview.notifyObservers(new AddBoxNodeCommand(palette, name,info));
            } else {
                JOptionPane.showMessageDialog(gview.getParent(), LabelReader.getInstance().getString("YouNeedToTypeANameForTheCustomBoxnode"), LabelReader.getInstance().getString("Error"), JOptionPane.ERROR_MESSAGE);
            }
        }else{
            JOptionPane.showMessageDialog(gview.getParent(), LabelReader.getInstance().getString("ThereIsNoUnboxedNodeInYourSelection"), LabelReader.getInstance().getString("Error"), JOptionPane.ERROR_MESSAGE);
        }
    }
}
